package org.iweb.web.json;

import java.io.Writer;

import org.iweb.html.impl.js.state.ElementState;
import org.iweb.html.impl.js.state.HtmlState;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
import com.thoughtworks.xstream.io.json.JsonWriter;

public class JsonUtil {
	
	private static XStream xstreamJson;
	
	static{
		xstreamJson = new XStream(new JsonHierarchicalStreamDriver() {
			
			@Override
			public HierarchicalStreamWriter createWriter(Writer out) {
				return new JsonWriter(out, JsonWriter.DROP_ROOT_MODE);
			}

		});
		xstreamJson.setMode(XStream.NO_REFERENCES);
		xstreamJson.alias("htmlstate", HtmlState.class);
		xstreamJson.alias("elementstate", ElementState.class);

	}

	public static String toJSON(Object obj) {
		return xstreamJson.toXML(obj);
	}
	
}
